package com.hkts.naturalgasauthentication.base.serviceimpl;

import com.hkts.naturalgasauthentication.base.dao.DepartmentDao;
import com.hkts.naturalgasauthentication.base.dao.DepartmentHeaderDao;
import com.hkts.naturalgasauthentication.base.dao.ManagementBaseDao;
import com.hkts.naturalgasauthentication.base.entity.Department;
import com.hkts.naturalgasauthentication.base.entity.DepartmentHeader;
import com.hkts.naturalgasauthentication.base.service.HontDepartmentService;
import com.hkts.naturalgasauthentication.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HontDepartmentServiceimpl implements HontDepartmentService {
    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private DepartmentHeaderDao departmentHeaderDao;
    @Override
    public DepartmentDao getRepository() {
        return departmentDao;
    }

    @Override
    public List<Department> findByParentIdOrderBySortOrder(String parentId, Boolean openDataFilter) {

        // 数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return departmentDao.findByParentIdAndIdInOrderBySortOrder(parentId, depIds);
        }
        return departmentDao.findByParentIdOrderBySortOrder(parentId);
    }

    @Override
    public List<Department> findByParentIdAndStatusOrderBySortOrder(String parentId, Integer status) {

        return departmentDao.findByParentIdAndStatusOrderBySortOrder(parentId, status);
    }

    @Override
    public List<Department> findByTitleLikeOrderBySortOrder(String title, Boolean openDataFilter) {

        // 数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return departmentDao.findByTitleLikeAndIdInOrderBySortOrder(title, depIds);
        }
        return departmentDao.findByTitleLikeOrderBySortOrder(title);
    }

    @Override
    public List<Department> departmentTree() {
        List<Department> list = findByParentIdOrderBySortOrder("0", false);

        for(int i=0;i<list.size();i++){
            List<Department> departments = cheldrenDepartment(list.get(i).getId());
            if(departments.size()>0){
                list.get(i).setChildrenDepartment(departments);
            }
            List<String> listMain=new ArrayList<String>();
            List<DepartmentHeader> byDepartmentIdAndType = departmentHeaderDao.findByDepartmentIdAndType(list.get(i).getId(), 0);
            for(int j=0;j<byDepartmentIdAndType.size();j++){
                listMain.add(byDepartmentIdAndType.get(j).getUserId());
            }
            list.get(i).setMainHeader(listMain);
            List<DepartmentHeader> viceDepartmentIdAndType = departmentHeaderDao.findByDepartmentIdAndType(list.get(i).getId(), 1);
            List<String> viceMain=new ArrayList<String>();
            for(int j=0;j<viceDepartmentIdAndType.size();j++){
                viceMain.add(viceDepartmentIdAndType.get(j).getUserId());
            }
            list.get(i).setViceHeader(viceMain);
        }

        return list;
    }

    @Override
    public List<Department> findByParentIdOrderBySortOrder(String parentId) {
        return departmentDao.findByParentIdOrderBySortOrder(parentId);
    }

    public List<Department> cheldrenDepartment(String id){
        List<Department> list = findByParentIdOrderBySortOrder(id, false);
        for(int i=0;i<list.size();i++){
            List<Department> departments = cheldrenDepartment(list.get(i).getId());
            if(departments.size()>0){
                list.get(i).setChildrenDepartment(departments);
            }
            List<String> listMain=new ArrayList<String>();
            List<DepartmentHeader> byDepartmentIdAndType = departmentHeaderDao.findByDepartmentIdAndType(list.get(i).getId(), 0);
            for(int j=0;j<byDepartmentIdAndType.size();j++){
                listMain.add(byDepartmentIdAndType.get(j).getUserId());
            }
            list.get(i).setMainHeader(listMain);
            List<DepartmentHeader> viceDepartmentIdAndType = departmentHeaderDao.findByDepartmentIdAndType(list.get(i).getId(), 1);
            List<String> viceMain=new ArrayList<String>();
            for(int j=0;j<viceDepartmentIdAndType.size();j++){
                viceMain.add(viceDepartmentIdAndType.get(j).getUserId());
            }
            list.get(i).setViceHeader(viceMain);
        }
        return list;
    }
}
